package crack;

import common.ListNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author fance
 * @date 18-4-1 上午10:52
 */
public class ListSum {
    public ListNode delDupList(ListNode list) {
        List<Integer> map  = new ArrayList<>();
        ListNode pre = null;
        ListNode head = list;
        while (list != null) {
            if (map.contains(list.val)) {
                pre.next = list.next;
            } else {
                map.add(list.val);
                pre = list;
            }
            list = list.next;
        }
        return head;
    }

    public ListNode nthToLast(ListNode head, int k) {
        if (k <= 0) {
            return null;
        }
        ListNode p1 = head;
        ListNode p2 = head;
        for (int i = 0; i < k - 1; i++) {
            if (p2 == null) {
                return null;
            }
            p2 = p2.next;
        }
        if (p2 == null) {
            return null;
        }
        while (p2.next != null) {
            p1 = p1.next;
            p2 = p2.next;
        }
        return p1;
    }

    public ListNode partition(ListNode head, int x) {
        ListNode beforeSt = null;
        ListNode beforeEnd = null;
        ListNode afterSt = null;
        ListNode afterEnd = null;
        ListNode tmp = head;
        while (tmp != null) {
            ListNode next = tmp.next;
            tmp.next = null;
            if (tmp.val < x) {
                if (beforeSt == null) {
                    beforeSt = tmp;
                    beforeEnd = beforeSt;
                } else {
                    beforeSt.next = tmp;
                    beforeEnd = tmp;
                }
            } else {
                if (afterSt == null) {
                    afterSt = tmp;
                    afterEnd = beforeSt;
                } else {
                    afterSt.next = tmp;
                    afterEnd = tmp;
                }
            }
            tmp = next;
        }
        if (beforeSt == null) {
            return afterSt;
        }
        beforeEnd.next = afterSt;
        return beforeSt;
    }

    public ListNode addLists(ListNode l1, ListNode l2) {
        return addListsHelper(l1, l2, 0);
    }
    public ListNode addListsHelper(ListNode l1, ListNode l2, int carry) {
        if (l1 == null && l2 == null && carry == 0) {
            return null;
        }
        ListNode res = new ListNode(0);
        int val = carry;
        if (l1 != null) {
            val += l1.val;
        }
        if (l2 != null) {
            val += l2.val;
        }
        res.val = val % 10;
        ListNode more = addListsHelper(l1 == null ? null : l1.next,
                l2 == null ? null : l2.next,
                val >= 10 ? 1 : 0);
        res.next = more;
        return res;
    }

    public ListNode findBegin(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        slow = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return fast;
    }

    public boolean isPalindrome(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        LinkedList<Integer> stack = new LinkedList<>();

        while (fast != null && fast.next != null) {
            stack.push(slow.val);
            slow = slow.next;
            fast = fast.next.next;
        }
        // 奇数个元素 跳过中间元素
        if (fast != null) {
            slow = slow.next;
        }
        while (slow != null) {
            if (stack.pop() != slow.val) {
                return false;
                // test 冲突
            }
            slow = slow.next;
        }
        return true;
    }
    /*{
        思路总结
       删除某个节点  复制后一个值  然后指向后后节点
    }*/
    @Test
    public void test() {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(1);
        ListNode l4 = new ListNode(4);
        ListNode l5 = new ListNode(5);
        ListNode l6 = new ListNode(6);
        l1.next = l2;
        l2.next = l3;
        l4.next = l5;
        l5.next = l6;


        System.out.println(isPalindrome(l1));
        System.out.println(isPalindrome(l4));

    }
}
